//
//  M4InfoScontactoParaView.swift
//  M4
//
//  Created by Blankfein Prestamos on 2025/8/2.
//

import UIKit

class M4InfoScontactoParaView: UIView {

    @IBOutlet weak var tiplabel: UILabel!
    
    var didTapAgregarHandle: (() -> Void)? = nil

    @IBOutlet weak var agregarbtn: UIButton!
    
    @IBAction func pressAgregarBtn(_ sender: UIButton) {
        
        didTapAgregarHandle?()
        
    }
    
    override  func awakeFromNib() {
        super.awakeFromNib()
        self.changcontrnt1(is3: false)
    }
    
    func addDashedBorder(to button: UIButton) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = UIColor.init(hex: 0x00479D) .cgColor        // 虚线颜色
        shapeLayer.fillColor = nil                          // 内部透明
        shapeLayer.lineDashPattern = [6, 3]                 // 6 实线 + 3 空格
        shapeLayer.lineWidth = 1                            // 线宽

        // 创建和按钮大小一致的圆角矩形路径
        let path = UIBezierPath(roundedRect: button.bounds, cornerRadius: button.layer.cornerRadius)
        shapeLayer.path = path.cgPath

        // 避免重复添加，先移除已有的虚线层
        button.layer.sublayers?.removeAll(where: { $0.name == "dashedBorder" })
        shapeLayer.name = "dashedBorder"

        button.layer.addSublayer(shapeLayer)
    }
    
    override  func layoutSubviews() {
        super.layoutSubviews()
        
        addDashedBorder(to: self.agregarbtn)
        
    }
    
    func changcontrnt1(is3:Bool){
        var fullText = "Felicitaciones, ha obtenido un 20% de crédito y agregar otro contacto aumentará su tasa de aprobación en otro 30%"
        if (is3){
            let attributedText = NSMutableAttributedString(string: fullText)

            let boldFont = UIFont.boldSystemFont(ofSize: 18)

            
            // 加粗 "30%"
            if let range30 = fullText.range(of: "30") {
                let nsRange = NSRange(range30, in: fullText)
                attributedText.addAttribute(.font, value: boldFont, range: nsRange)
            }

            // 设置到 UILabel
            tiplabel.attributedText = attributedText
        }else{
            
            fullText = "Añade un contacto para aumentar tu crédito en un 20%"
            let attributedText = NSMutableAttributedString(string: fullText)

            let boldFont = UIFont.boldSystemFont(ofSize: 18)

            // 加粗 "20%"
            if let range20 = fullText.range(of: "20") {
                let nsRange = NSRange(range20, in: fullText)
                attributedText.addAttribute(.font, value: boldFont, range: nsRange)
            }

           

            // 设置到 UILabel
            tiplabel.attributedText = attributedText
        }
        

        
    }

}
